<?php
namespace Common\Service;
/**
 * 设备操作
 * @author sunny5156 <137898350@qq.com>
 * @version
 */
class OperationService extends CommonService{
    
    private $_monitorId = 0;
    private $_monitorConfig = array();
    private $_monitorStatus = 0;
    private $_serviceUrl = ''; //服务地址
    private $_soapClient = ''; //服务客户端
    private $_monitorModel = '';
    private $_deviceModel = '';
    
    
    public function __construct($config = array('monitor_id'=>20)){
    
        $this->_serviceUrl = C('WEB_SERVICE.URL');
        try{
            $this->_soapClient = new \SoapClient($this->_serviceUrl);
        }catch (\SoapClient $e){
            exit('0');
        }
        
        $this->_monitorId = $config['monitor_id'];
        
        $this->_monitorModel = D('Monitor');
        $this->_deviceModel = D('Device');
        
        $this->checkMonitorIsRun();
        
        
    }
    
    
    /**
     * 检测服务是否启动
     *
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午9:59:18
     */
    public function checkServiceIsRun(){
    
//         if(isset($_SESSION['service'])){
//             $status = session('service');
//         }else{
//             $res = $this->_soapClient->CntConTest(array('code'=>'1111','password'=>'young_king' ,'commType'=>'3', 'ComPort'=>'3', 'TelNum'=>''));
    
//             $status = intval($res->CntConTestResult);
//             session('service',$status);
//         }
    
//         return $status;
    }
    /**
     * 检测集中器是否运行
     *
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:16:01
     */
    public function checkMonitorIsRun(){
        if(!$this->_monitorId){
            exit('请填写集中器ID');
        }
        
        $this->_monitorConfig = $this->_monitorModel->field('comtype as modes,comport as ports,dialpassword as passwords,dialnumber as numbers,name,state')
                                                    ->where(array('id'=>$this->_monitorId))->find();
        $this->_monitorStatus = $this->_monitorConfig['state'];
        unset($this->_monitorConfig['state']);
        
        //if($this->_monitorStatus){
            $this->startMonitor();
        //}
    }
    /**
     * 连接集中器
     *
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:02:51
     */
    private function startMonitor(){
        //debug($this->_monitorConfig);
        if(empty($this->_monitorConfig)){
            exit('查不到集中器');
        }
        try {
            $res = $this->_soapClient->Connect($this->_monitorConfig);
        }catch (\Exception $e){
            return false;
        }
        if(!$res->ConnectResult){
            exit('集中器连接失败');
        }
    
        return $this->_monitorStatus = $res->ConnectResult == 1?true:false;
    }
    /**
     * 断开集中器
     * @param array $param
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:04:37
     */
    public function stopMonitor($param = array('monitor_id'=>10)){
        $res = $this->_soapClient->Disconnect(array('concentratorID'=>$param['monitor_id'],'keepAlive'=>true));
    
        //session('monitorStatus'.$param['monitor_id'],$res->StopMonitorResult == 1?1:0);
    
        $res->DisconnectResult == 1?true:false;
        
        if($res->DisconnectResult){
            $res = M('Devicesets')->where(array('id'=>$this->_monitorId))->setField('state',0);
        }
    }
    
    public function checkDevice(){
    
    }
    
    /**
     * 启动设备
     * @param unknown $param
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:12:38
     */
    public function startDevice($param){
    
        if(!$param['device_id']){
            exit('参数缺少');
        }
    
        $deviceData = $this->_deviceModel->getDevice($param['device_id']);
        
    
        $i = 0;
        //do {
        $res = $this->_soapClient->MeterSupply(array('meterNum'=>$deviceData['code']));
        //    $i++;
        $res = $res->MeterSupplyResult;
        //}while ( $res == true);
        if($res){
            $this->_deviceModel->where(array('id'=>$param['device_id']))->save(array('is_run'=>1));
        }
    
        return $res == 1?true:false;
    }
    /**
     * 停止设备
     *
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:12:46
     */
    public function stopDevice($param){
        if(!$param['device_id']){
            exit('参数缺少');
        }
    
        $deviceData = $this->_deviceModel->getDevice($param['device_id']);
    
        $i = 0;
        //do {
        $res = $this->_soapClient->MeterCut(array('meterNum'=>$deviceData['code']));
        //    $i++;
        $res = $res->MeterCutResult;
        //}while ( $res == true);
        if($res){
            $this->_deviceModel->where(array('id'=>$param['device_id']))->save(array('is_run'=>0));
        }
    
        return $res == 1?true:false;
    }
    /**
     * 读取设备基础数据
     * @param unknown $param
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 上午11:15:32
     */
    public function readDeviceData($param){
        if(!$param['device_id']){
            exit('参数缺少');
        }
    
        $deviceData = $this->_deviceModel->getDevice($param['device_id']);
    
        $i = 0;
        //do {
        $res = $this->_soapClient->ReadMeterCurrentBase(array('meterNumS'=>$deviceData['code']));
        //    $i++;
        $res = $res->ReadMeterCurrentBaseResult;
        //}while ( $res == true);
    
        return $res;
    }
    /**
     * 开关
     * @param unknown $param
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 下午4:27:14
     */
    public function openDevice($param){
    
        //全关0
        //     $ValveFullClose = $c->ValveFullClose(array('deviceID'=>22));
        //全开3
        //     $ValveFullOpen = $c->ValveFullOpen(array('deviceID'=>22));
    
        //开三分之一 1
        //     $ValveOpenWith1Per3 = $c->ValveOpenWith1Per3(array('deviceID'=>22));
    
        //开三分之二 2
        //$ValveOpenWith2Per3 = $c->ValveOpenWith2Per3(array('deviceID'=>22));
    
        $type = $param['type'];
    
        $result = '';
    
        switch ($type){
            case 'full':
                $res = $this->_soapClient->ValveFullOpen(array('deviceID'=>$param['device_id']));
                $result = $res->ValveFullOpenResult;
                break;
            case '1/3':
                $res = $this->_soapClient->ValveOpenWith1Per3(array('deviceID'=>$param['device_id']));
                $result = $res->ValveOpenWith1Per3Result;
                break;
            case '2/3':
                $res = $this->_soapClient->ValveOpenWith2Per3(array('deviceID'=>$param['device_id']));
                $result = $res->ValveOpenWith2Per3Result;
                break;
        }
    
        return $result?1:0;
    
    }
    /**
     * 关闭设备
     * @param unknown $param
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年3月3日 下午4:29:58
     */
    public function closeDevice($param){
        $type = $param['type'];
        $res = $this->_soapClient->ValveFullClose(array('deviceID'=>$param['device_id']));
    
        return $res->ValveFullCloseResult?1:0;
    }
    /**
     * 设备报警开
     * @param unknown $param
     * @return number
     * @author sunny5156<137898350@qq.com>
     * @version 
     * @todo  2016年4月7日 下午6:20:53
     */
    public function deviceAlarmOn($param){
        if(!$param['device_id']){
            exit('参数缺少');
        }
    
        $deviceData = $this->_deviceModel->getDevice($param['device_id']);
    
        $i = 0;
        //do {
        $res = $this->_soapClient->MeterAlarmOn(array('meterNum'=>$deviceData['code'],'isRun'=>$deviceData['is_run']));
        //    $i++;
        $res = $res->MeterAlarmResult;
        //}while ( $res == true);
        if($res){
            $this->_deviceModel->where(array('id'=>$param['device_id']))->save(array('is_alarm'=>1));
        }
    
        return $res == 1?true:false;
    }
    /**
     * 设备报警关
     * @param unknown $param
     * @return number
     * @author sunny5156<137898350@qq.com>
     * @version
     * @todo  2016年4月7日 下午6:20:53
     */
    public function deviceAlarmOff($param){
        if(!$param['device_id']){
            exit('参数缺少');
        }
    
        $deviceData = $this->_deviceModel->getDevice($param['device_id']);
    
        $i = 0;
        //do {
        $res = $this->_soapClient->MeterAlarmOff(array('meterNum'=>$deviceData['code'],'isRun'=>$deviceData['is_run']));
        //    $i++;
        $res = $res->MeterAlarmResult;
        //}while ( $res == true);
        if($res){
            $this->_deviceModel->where(array('id'=>$param['device_id']))->save(array('is_alarm'=>0));
        }
    
        return $res == 1?true:false;
    }
}